package com.wanxi.udpsendreceive;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receive {
    public static void main(String[] args) {
        FileOutputStream fileOutputStream = null;
        DatagramSocket datagramSocket = null;
        try {
            System.out.println("接收端启动");

            datagramSocket = new DatagramSocket(1948);

            byte[] bytes = new byte[1024 * 64];

            DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);

            datagramSocket.receive(datagramPacket);

            System.out.println("接收完毕");


            File file = new File("testrece.jpg");

            fileOutputStream = new FileOutputStream(file);

            fileOutputStream.write(datagramPacket.getData(), 0, datagramPacket.getLength());


            System.out.println("写入完毕");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (datagramSocket != null)
                    datagramSocket.close();
                if (fileOutputStream != null)
                    fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
